from google.appengine.ext import db

from gaeo.controller import BaseController

from model.user import User

class UserController(BaseController):
    def create(self):
        r = User(
            account = self.params.get('account', None),
            nick = self.params.get('nick', None),
        )
        r.put()
        self.redirect('/user')

    def destroy(self):
        r = User.get(self.params.get('key'))
        if r is not None:
            r.delete()
        self.redirect('/user')

    def edit(self):
        rec = User.get(self.params.get('key'))
        if rec:
            setattr(self, 'account', rec.account)
            setattr(self, 'nick', rec.nick)
        else:
            setattr(self, 'account', 'Error')
            setattr(self, 'nick', 'Error')

    def index(self):
        query = User.all()
        self.result = query

    def new(self):
        pass

    def show(self):
        rec = User.get(self.params.get('key'))
        if rec:
            setattr(self, 'account', rec.account)
            setattr(self, 'nick', rec.nick)
        else:
            setattr(self, 'account', 'Error')
            setattr(self, 'nick', 'Error')

    def update(self):
        r = User.get(self.params.get('key'))
        r.account = self.params.get('account', None)
        r.nick = self.params.get('nick', None)
        r.put()
        self.redirect('/user')
